package com.jmcgg.freesky.common.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class TudouUtil extends FreeskyUtil {
	// parameters from user input
	private String flvDir = "D:\\Xunlei\\神探狄仁杰3\\";
	private String indexFile = "D:\\Xunlei\\神探狄仁杰3\\神探狄仁杰3.htm";
	private String lid = "3279972"; // list id of doudan
	private String titleKeyword = "神探狄仁杰第三部";
	// end of parameters
	private String keyword = "lid=" + lid + "&iid="; // lid=3279972&iid=
	String[] exts = { "flv" };
	private HashMap<String, String> hashFlv = new HashMap<String, String>();

	public void getFlvList() {
		try {
			String line = null;
			String iid = null;
			String title = null;
			int idx = -1;
			hashFlv.clear();

			List<String> list = FileUtils.readLines(new File(indexFile));
			for (int i = 0; i < list.size(); i++) {
				line = list.get(i);
				idx = line.indexOf(keyword);
				if (idx > 0) {
					// found the keyword
					// get the remaining line after keyword
					line = line.substring(idx + keyword.length());
					// get the iid from the line
					iid = line.substring(0, line.indexOf("\""));
					// get the remaining line after title keyword
					line = line.substring(line.indexOf(titleKeyword));
					// get the title from line
					title = line.substring(0, line.indexOf("\""));
					hashFlv.put(iid, title);
					System.out.println("iid=" + iid + ";title=" + title);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void renameFiles() {
		String fileName = null;
		try {
			Iterator<File> iters = FileUtils.iterateFiles(
					new File(this.flvDir), exts, false);
			while (iters.hasNext()) {
				File file = iters.next();
				fileName = file.getName();
				fileName = fileName.substring(0, fileName.indexOf("."));
				if (hashFlv.containsKey(fileName)) {
					File dest = new File(flvDir
							+ hashFlv.get(fileName).toString() + ".flv");
					file.renameTo(dest);
					System.out.println(file.getAbsolutePath() + " --> "
							+ dest.getAbsolutePath());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// args: flvDir, indexFile, lid, titleKeyword
		TudouUtil util = new TudouUtil();
		if (args.length == 4) {
			util.setFlvDir(args[0]);
			util.setIndexFile(args[1]);
			util.setLid(args[2]);
			util.setTitleKeyword(args[3]);
			System.out.println("flvDir=" + args[0]);
			System.out.println("indexFile=" + args[1]);
			System.out.println("lid=" + args[2]);
			System.out.println("titleKeyword=" + args[3]);
		} else if (args.length > 0 && args.length != 4) {
			System.out.println("Usage:");
			System.out
					.println("java cn.afss.freesky.util.TudouUtil flvDir indexFile lid titleKeyword");
			System.out.println("Sample:");
			System.out
					.println("java cn.afss.freesky.util.TudouUtil \"D:\\Xunlei\\神探狄仁杰3\\\" \"D:\\Xunlei\\神探狄仁杰3\\神探狄仁杰3.htm\" \"3279972\" \"神探狄仁杰第三部\"");
			return;
		}
		System.out.println("Start TudouUtil... ");
		util.getFlvList();
		util.renameFiles();
		System.out.println("TudouUtil Finished!");
	}

	public String getFlvDir() {
		return flvDir;
	}

	public void setFlvDir(String flvDir) {
		this.flvDir = flvDir;
	}

	public String getIndexFile() {
		return indexFile;
	}

	public void setIndexFile(String indexFile) {
		this.indexFile = indexFile;
	}

	public String getLid() {
		return lid;
	}

	public void setLid(String lid) {
		this.lid = lid;
	}

	public String getTitleKeyword() {
		return titleKeyword;
	}

	public void setTitleKeyword(String titleKeyword) {
		this.titleKeyword = titleKeyword;
	}

}
